﻿using System;
using AMM.Business;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;

namespace AMM.UserControls {
    public partial class BankAccountEditControl : BaseEditControl {

        private FlexBindingSource<BankAccount> _fbs;

        public override object BaseEditModel {
            get { return this._fbs.CurrentItem; }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<BankAccount>(); }
                this._fbs.CurrentItem = (BankAccount)value;
            }
        }

        public BankAccountEditControl() {
            this.InitializeComponent();
        }

        private void BankAccountEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<BankAccount>(); }
            this.BindBasics(this.cmbCurrency, BasicsType.Currency);
            this.BindBasics(this.cmbFinancialAccountType, BasicsType.FinancialAccountType);
            this.CreateFormBindings();
            this.BindValidation();
        }               
        
        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.tbCode, x => x.Text, y => y.Code);
            this._fbs.CreateBinding(this.tbBankName, x => x.Text, y => y.BankName);
            this._fbs.CreateBinding(this.cmbCurrency, x => x.SelectedValue, y => y.CurrencyId);
            this._fbs.CreateBinding(this.cmbFinancialAccountType, x => x.SelectedValue, y => y.FinancialAccountTypeId);
            this._fbs.CreateBinding(this.tbBankAccountName, x => x.Text, y => y.BankAccountName);
            this._fbs.CreateBinding(this.tbBankAccountNumber, x => x.Text, y => y.BankAccountNumber);
            this._fbs.CreateBinding(this.dtpOpenedDate, x => x.Value, y => y.OpenedDate);
            this._fbs.CreateBinding(this.tbRemarks, x => x.Text, y => y.Remarks);
            this._fbs.CreateBinding(this.cbIsEnabled, x => x.Checked, y => y.IsEnabled);
        }
        
        private void BindValidation() {
            this.ValidatorForEdit.SetType(this.tbCode, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbCode, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbCode, "^[A-Za-z0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbCode, "编码只能由字母，数字，连接线组成");

            this.ValidatorForEdit.SetType(this.tbBankAccountName, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbBankAccountName, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbCurrency, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbCurrency, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbFinancialAccountType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbFinancialAccountType, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbBankName, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbBankName, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbBankAccountNumber, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.tbBankAccountNumber, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.dtpOpenedDate, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.dtpOpenedDate, this.RequiredMessage);
        }
    }
}
